package com.junmeng.jdemo.recycler;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.common.recycler.decoration.BaseLayoutOrderItemDecoration;
import com.junmeng.jdemo.R;

/**
 * 演示了使用View作为分割线的用法，这样可以省去繁琐的绘制代码
 */
public class ViewItemDecoration extends BaseLayoutOrderItemDecoration {

    private TextView mTextView;

    public ViewItemDecoration() {
    }

    @Override
    public int getItemDecorationLayoutResId() {
        return R.layout.item_simple;
    }

    @Override
    public void initItemDecorationLayoutViews(@NonNull View itemView) {
        mTextView = itemView.findViewById(R.id.text);
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.top = getItemDecorationHeight();
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        Log.i("123456","onDraw");
        int count = parent.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = parent.getChildAt(i);
            setCurrentItemDecorationRestMeasureWidth(getCurrentOrderDecoratedMeasuredWidth(child, parent, state));
//            mTextView.setText((i * i) + "");
            mTextView.setText("123");
            notifyItemDecorationChanged();
            drawDecorationViewOnLeftOrTop(c, child, parent, state);
        }

    }

    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
//        Log.i("123456","onDrawOver");
    }
}
